package com.aptech.action;

import com.aptech.business.CustomerBusiness;
import com.aptech.dto.Customer;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ModifyCustomerAction extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String des = request.getParameter("des");

            Customer cust = CustomerBusiness.getCustomerByName(id);
            if (cust != null) {
                cust.setCustName(name);
                cust.setDescription(des);
                CustomerBusiness.updateCustomer(cust);
                request.setAttribute("customer_msg", "You have updated customer '" + name + "' information");
                request.getRequestDispatcher("/getCustomerList").forward(request, response);
            } else {
                request.setAttribute("customer_err", "Customer doesn't exist");
                request.getRequestDispatcher("/customer/modifyCustomer.jsp").forward(request, response);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
            Logger.getLogger(ModifyCustomerAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
